﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;

namespace Org.Eurekaa.DNJ.UI.DNJSettings
{
    
    [Serializable()]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class GenericSettingsObject
    {

        private Hashtable _settings = new Hashtable();
        
        [Browsable(false)]        
        public Hashtable Settings
        {
            get { return _settings; }            
        }


        public object this[object key]
        {
            get
            {
                return _settings[key];
            }
            set
            {
                _settings[key] = value;
            }
        }

        public T Get<T>(string key)
        {
            object v = _settings[key];
            //if (v == null) return default(T);
            try
            {
                return (T)v;
            }
            catch {
                return default(T);
            }
        }
        public override string ToString()
        {
            string str = "";
            foreach (object key in _settings.Keys)
            {
                //object val = _settings[key];
                string objVal = ToStrByType(_settings[key]);
                if (!String.IsNullOrEmpty(objVal))
                {

                    if (String.IsNullOrEmpty(str)) str = "{";
                    else str += ",";

                    str += key.ToString() + ":";
                    str += objVal;
                }
            }
            if (!String.IsNullOrEmpty(str)) str += "}";

            return str;
        }

        private String ToStrByType(object obj)
        {
            if (obj is String || obj is Enum)
            {
                string strObj = obj.ToString();
                if (strObj.StartsWith("{") && strObj.EndsWith("}"))
                    return strObj.Substring(1, strObj.Length - 2);
                return "'" + strObj + "'";
            }
            if (obj is Boolean) return obj.ToString().ToLower();
            if (obj is ArrayList)
            {
                ArrayList ar = (obj as ArrayList);
                if (ar.Count == 0) return "null";
                string str = "[";
                foreach (object o in ar)
                {
                    string tmp = ToStrByType(o);
                    if (tmp.Equals("-1")) continue;
                    if (!str.EndsWith("[")) str += ",";

                    str += tmp;
                }
                if (str == "[") return "null";
                str += "]";
                return str;
            }
            

            return obj.ToString();
        }


        public void Append(GenericSettingsObject objToAdd)
        {
            foreach (object key in objToAdd.Settings.Keys)
            {
                if (!Settings.ContainsKey(key))
                    Settings.Add(key, objToAdd.Settings[key]);  
            }


            //Hashtable objSettings = new Hashtable(objToAdd.Settings);
            //Hashtable newSettings = new Hashtable(Settings);
            //ICollection currentKeys = objSettings.Keys;
            //foreach (object key in currentKeys)
            //{
            //    if (!newSettings.ContainsKey(key))
            //        newSettings.Add(key, objSettings[key]);
            //}

            //_settings = newSettings;


        }

    }
}
